package com.oscar.ehome.view.setview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.UsersBean;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.bean.setbean.AlipayBean;
import com.oscar.ehome.bean.setbean.PaySuccessBean;
import com.oscar.ehome.bean.setbean.WeChatBean;
import com.oscar.ehome.presenter.SetPresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.setview.view.PayView;


import org.apache.commons.lang.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PaysuccesActivity extends BasesActivity<PayView, SetPresenter> implements PayView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;

    /**
     * 支付成功图片
     */
    @BindView(R.id.pay_sussec_img)
    ImageView pay_sussec_img;

    @BindView(R.id.pays_price)
    TextView paysPrice;
    @BindView(R.id.pays_way)
    TextView paysWay;
    @BindView(R.id.pays_time)
    TextView paysTime;
    @BindView(R.id.pays_show)
    TextView paysShow;
    @BindView(R.id.pays_name)
    TextView paysName;
    @BindView(R.id.pays_phone)
    TextView paysPhone;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    SetPresenter setPresenter;

    UsersBean bean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paysucces);
        ButterKnife.bind(this);
        setColor();
        initdata();
        initView();
    }

    @Override
    protected SetPresenter createPresenter() {
        setPresenter = new SetPresenter(this);
        return setPresenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(PaysuccesActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(pay_sussec_img, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    public void initView() {

        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("支付");

        String user = SystemConfigFactory.getInstance(PaysuccesActivity.this).getSystemConfig().getUserMessage();
        bean = new Gson().fromJson(user, UsersBean.class);

        String users = SystemConfigFactory.getInstance(PaysuccesActivity.this).getSystemConfig().getUserBean();
        UserBean usersbean = new Gson().fromJson(users, UserBean.class);

        if (bean != null) {
            String payname = bean.getName();
            String payphone = usersbean.getMobile();
            paysName.setText(payname);
            paysPhone.setText(payphone);
        }
    }

    public void initdata() {

        setPresenter.getUserPayInfo(SystemConfigFactory.getInstance(PaysuccesActivity.this).getSystemConfig().getToken());
        showLoading();

    }

    //未用
    @Override
    public void getWeChat(WeChatBean weChatBean) {

    }

    //未用
    @Override
    public void getAlipay(AlipayBean alipayBean) {

    }

    @Override
    public void getUserPayInfo(PaySuccessBean paySuccessBean) {
        dismissLoading();
        Log.e("money", "getUserPayInfo: "+"   "  +paySuccessBean.getPrice() );
        paysPrice.setText(paySuccessBean.getPrice() + "元");

        if (paySuccessBean.getPayWay().equals("offline")) {
            paysWay.setText("线下支付");
        } else if (paySuccessBean.getPayWay().equals("alipay")) {
            paysWay.setText("支付宝支付");
        } else if (paySuccessBean.getPayWay().equals("wxpay")) {
            paysWay.setText("微信支付");
        }
        paysTime.setText(paySuccessBean.getPayTime() + "");
        if (StringUtils.isEmpty(paySuccessBean.getPayDesc())) {
            paysShow.setText("暂无");
        } else {
            paysShow.setText(paySuccessBean.getPayDesc() + "");
        }

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(PaysuccesActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(PaysuccesActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        CommonUtils.pay = 2;
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            CommonUtils.pay = 2;
        }
        return super.onKeyDown(keyCode, event);
    }
}
